package com.example.handheld4_gsignal_app.ui.fragments;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.handheld4_gsignal_app.R;
import com.example.handheld4_gsignal_app.services.SettingsService;

/**
 * 设置Fragment - 应用设置和配置
 */
public class SettingsFragment extends Fragment {

    private TextView versionText;
    private TextView deviceInfoText;
    private Button clearDataButton;
    private Button exportDataButton;
    private Button aboutButton;
    private SettingsService settingsService;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_settings, container, false);
        
        initViews(view);
        setupClickListeners();
        initSettingsService();
        loadSettingsData();
        
        return view;
    }

    /**
     * 初始化视图组件
     */
    private void initViews(View view) {
        versionText = view.findViewById(R.id.version_text);
        deviceInfoText = view.findViewById(R.id.device_info_text);
        clearDataButton = view.findViewById(R.id.clear_data_button);
        exportDataButton = view.findViewById(R.id.export_data_button);
        aboutButton = view.findViewById(R.id.about_button);
    }

    /**
     * 设置点击事件监听器
     */
    private void setupClickListeners() {
        clearDataButton.setOnClickListener(v -> clearAllData());
        exportDataButton.setOnClickListener(v -> exportData());
        aboutButton.setOnClickListener(v -> showAboutDialog());
    }

    /**
     * 初始化设置服务
     */
    private void initSettingsService() {
        settingsService = new SettingsService(getContext());
    }

    /**
     * 加载设置数据
     */
    private void loadSettingsData() {
        if (settingsService != null) {
            versionText.setText("版本: " + settingsService.getAppVersion());
            deviceInfoText.setText("设备: " + settingsService.getDeviceInfo());
        }
    }

    /**
     * 清除所有数据
     */
    private void clearAllData() {
        if (settingsService != null) {
            settingsService.clearAllData();
            Toast.makeText(getContext(), "数据已清除", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 导出数据
     */
    private void exportData() {
        if (settingsService != null) {
            String exportPath = settingsService.exportData();
            if (exportPath != null) {
                Toast.makeText(getContext(), "数据已导出到: " + exportPath, Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getContext(), "导出失败", Toast.LENGTH_SHORT).show();
            }
        }
    }

    /**
     * 显示关于对话框
     */
    private void showAboutDialog() {
        if (settingsService != null) {
            String aboutInfo = settingsService.getAboutInfo();
            Toast.makeText(getContext(), aboutInfo, Toast.LENGTH_LONG).show();
        }
    }
}